import Mock from 'mockjs';

Mock.setup({
  timeout: '200-600'
});

const load = (collection) => {
  collection.map(({
    path,
    method,
    handle
  }) => {
    if (method === '*') {
      method = [
        'get',
        'head',
        'post',
        'put',
        'delete',
        'connect',
        'options',
        'trace',
        'patch'
      ]
    }
    if (typeof method === 'string' && method.indexOf('|') > -1) method = method.split('|');
    if (method instanceof Array) {
      method.map(item => setup(path, item, handle));
    } else {
      setup(path, method, handle);
    }
  })
}

const req = context => context.keys().map(context);
const options = req(require.context('./api/', true, /\.js$/))
  .filter(e => e.default)
  .map(e => e.default);

options.forEach(option => load(option));
